import { defineStore } from 'pinia';
import * as productApi from '@/api/product';

export const useProductStore = defineStore('product', () => {
	async function getProductListByCoupon(param?: any) {
		const response = await productApi.getProductListByCoupon(param);
		if (response.code === 200) {
			return response.data.items;
		}
		return [];
	}

	async function getProductByCoupon(param?: any) {
		const response = await productApi.getProductByCoupon(param);
		if (response.code === 200) {
			return response.data;
		}
		return {};
	}

	async function getCategories(param?: any) {
		uni.showLoading();
		const response = await productApi.getCategories(param);
		uni.hideLoading();
		if (response.code === 20000) {
			return response.data.categories;
		}
		return [];
	}

	async function getProducts(param?: any) {
		uni.showLoading();
		const response = await productApi.getProducts(param);
		uni.hideLoading();
		if (response.code === 20000) {
			return response.data.products;
		}
		return [];
	}

	async function getProduct(param?: any) {
		uni.showLoading();
		const response = await productApi.getProduct(param);
		uni.hideLoading();
		if (response.code === 20000) {
			return response.data.product;
		}
		return {};
	}

	return {
		getProductListByCoupon,
		getProductByCoupon,
		getCategories,
		getProducts,
		getProduct
	};
});
